package com.wangshuqi.jdbc.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * jdbc 连接
 *
 * @author codewsq
 * @date 2021/8/20 0020 12:04
 */
public class JDBCConnectionTest {

    // 数据库连接方式一：代码中显式出现了第三方数据库的API，耦合度高
    @Test
    public void test1() throws SQLException {
        // 1.实例化driver，
        Driver driver = new com.mysql.jdbc.Driver();

        // 2.提供url指明具体的操作数据库
        String url = "jdbc:mysql://localhost:3306/test";

        // 3.提供Properties的对象，指明用户名和密码
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");

        // 4.调用driver的connect(),获取连接
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    // 方式二：相较于方式一，这里使用反射实例化Driver，不在代码中体现第三方数据库的API。体现了面向接口编程思想。
    @Test
    public void test2() throws Exception {
        // 1.实例化driver
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        // 2.提供url指明具体的操作数据库
        String url = "jdbc:mysql://localhost:3306/test";

        // 3.提供Properties的对象，指明用户名和密码
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");

        // 4.调用driver的connect(),获取连接
        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    // 方式三：使用DriverManager实现数据库的连接。
    @Test
    public void test3() throws Exception {
        // 1.数据库连接4个基本要素
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        // 2.加载数据库驱动
        Class clazz = Class.forName(driverName);
        Driver driver = (Driver) clazz.newInstance();

        // 3.注册驱动
        DriverManager.registerDriver(driver);

        // 4.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    // 方式四：简化方式三，不必显式的注册驱动了。因为在DriverManager的源码中已经存在静态代码块，实现了驱动的注册。
    @Test
    public void test4() throws ClassNotFoundException, SQLException {
        // 1.数据库连接4个基本要素
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";

        // 2.加载数据库驱动，在com.mysql.jdbc.Driver类的静态代码块已经进行了驱动注册的操作，
        //                当通过Class.forName()方法加载类时，会运行该类的静态代码块
        Class.forName(driverName);

        // 3.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    @Test
    public void test5() throws IOException, SQLException, ClassNotFoundException {
        // 1.加载配置文件
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);

        // 2.读取配置信息
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        // 3.加载驱动
        Class.forName(driver);

        // 4.获取数据库连接
        Connection connection = DriverManager.getConnection(url, user, password);
//        Connection connection = DriverManager.getConnection(url, properties);  // 这种方式也可以
        System.out.println(connection);
    }
}
